var type = RequestURLParam.getParam("type")
    ,urlTxt = "/sys-role/add"
    ,sysRole
    ,form;

$(function(){
    if(type && type === 'edit'){
        init_data();
        urlTxt = "/sys-role/update";
    } else {
        sysRole = new Object();
    }

    document.getElementById('roleName').focus();
});

layui.use(['form'], function(){
    form = layui.form;

    form.verify({
        roleName: function (value) {
            if (!value || value.length == 0) {
                return '请输入角色名称，角色名称为必须入力项目。'
            }
        },
    });

    //监听提交
    form.on('submit(checkData)', function(data){
        submit_data(data);
        return false;
    });
});

function init_data(){
    sysRole = JSON.parse(localStorage.sysRole);
    localStorage.removeItem('sysRole');

    $(".layui-input").each(function () {
        $(this).val(sysRole[this.id])
    });
}

function submit_data(reqVal){
    sysRole.roleName = reqVal.field.roleName;
    sysRole.roleRemark = reqVal.field.roleRemark;
    ActHttpRequest(urlTxt, sysRole, function (data) {
        if (data.code == 0) {
            parent.layer.msg('成功！', {icon: 1,time:800});
            parent.reload();
        } else {
            parent.layer.alert(data.msg);
            document.getElementById('roleName').focus();
            return;
        }

        var thisindex = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(thisindex);
    });
}